
import Round from "./basics/Round.js"
import Row from "./basics/Row.js"
import Block from "./basics/Block.js"
/*const  Round=qguan("round");
let Row =qugan("Row")
let Block =qugan("Block")*/
export default class  TreeData {

    constructor(){

        this.data=new Round()
        this.data.addBlock(new Block())

        this.data.addRow(new Row());


    }
    findStru(){
         return    this.data.list[0].list
    }

    addData(row,data){
        this.data.list[0].list.push(row);
        this.data.list.push(data)
    }

    find(index){
        return this.data.list[index]
    }

    merge(treeA,treeB,contrast){
        this.treeA=treeA;
        this.treeB=treeB;

        this.struA=treeA.findStru()
        this.struB=treeB.findStru()
        this.contrast=contrast;
       this. ergodic (1,1)
    }

    /**
     *   遍历树
     * @param w
     * @returns {Array}
     */
    ergodic (idA,idB) {
        let _this=this;
        let   funcs= _this.findAlikeB(idA,idB)
        if (this.struA[idA].isZero(2)) {
            if (!this.struA[idA].isZero(3)) {
                    this. ergodic(this.struA[idA].find(3),this.struB[funcs.idB].find(3));
            }
        } else {
            this. ergodic(this.struA[idA].find(2),this.struB[funcs.idB].find(2))
            if (!this.struA[idA].isZero(3)) {
                    this. ergodic(this.struA[idA].find(3),this.struB[funcs.idB].find(3));
            }

        }
        if(!this.struB[idB].isZero(2)){
            _this.notAlikeB(idA,idB)
        }



    }

    /**
     * 查找相同的B
     */
    findAlikeB(idA,idB){
        let _this=this;
        let row =this.struB[idB];
        let pidB=row.find(1)
        for(let i=1;i<this.struB.length;i++){

            if(this.struB[i].isValue(1,pidB)){
                if(this.contrast(this.treeA.find(idA),this.treeB.find(i))){
                    return new MergeTreeFuncs(idA,i,_this)
                } ;
            }

        }

        return new MergeTreeFuncs(idA,0,this);
    }



    /**
     * 遍历B存在，A不存在
     */
    notAlikeB(pidA,idB){

        let keyB=this.struB[idB].find(4)
        let keyA=this.struA[pidA].find(4)
        for(let i=1;i<this.struB.length;i++){
            if(this.struB[i].isValue(1,keyB)){
                let existA=true;

                for(let l=1;l<this.struA.length;l++){
                    if(this.struA[l].isValue(1,keyA)) {
                        if(this.contrast(this.treeA.find(i),this.treeB.find(l))){
                            existA=false;
                            break;
                        }
                        ;
                    }
                }
                if(existA){
                    new MergeTreeFuncs(0,i,this,keyA);
                }
            }

        }
    }


    /**
     *
     * 遍历 B
     */
    ergodicB(idB){
        let _this=this;
        if (_this.struB[idB].isZero(2)) {
            if (!this.struB[idB].isZero(3)) {
                new MergeTreeFuncs(0,idB,this);
                this. ergodic(this.struB[idB].find(3));

            }

        } else {
            new MergeTreeFuncs(0,idB,this);
                this. ergodic(this.struB[idB].find(2))
            if (!this.struB[idB].isZero(3)) {
                new MergeTreeFuncs(0,idB,this);
                    this. ergodic(this.struB[idA].find(3));


            }
        }
    }

    toTow(){
        for (let i=1  ;i < this.data.list[0].list.length;i++) {
            this.data.list[0].list[i].save(2,this.toL(this.data.list[0].list[i].find(4))) ;  //就是获取每个对象的menu_id
            this.data.list[0].list[i].save(3,this.toR(this.data.list[0].list[i].find(1),i)) ;//就是获取每个对象的pid
        }
    }
    toL (key) {
        for (let i=1  ;i < this.data.list[0].list.length;i++) {
            if (this.data.list[0].list[i].find(1) == key) {
                return i;
            }
        }
        return 0;
    }
    toR (pId,l) {
        for (var i = l; i < this.data.list[0].list.length; i++) {
            if (this.data.list[0].list[i].find(1)== pId && i != l) {
                return i;
            }
        }
        return 0;
    }




}



class  MergeTreeFuncs{
    constructor(idA,idB,pThis,pid){

        this.isErgodic=false; //是否继续遍历
        this.idA=idA;
        this.idB=idB;
        this.resData=new Array();
        this.row=new Row();
        this.pThis=pThis;
        if(idA!==0&&idB!==0){
            this.alike(idA,idB,pThis)
        }
        if(idA!==0&&idB===0){
            this.haveA(idA,idB,pThis)
        }
        if(idA===0&&idB!==0){
            this.haveB(idA,idB,pThis,pid)
        }
    }

    /**
     * 对比
     */
    static  contrast(idA,idB,pThis){
        if(pThis.treeA.find(idA).name==pThis.treeB.find(idB).name){
            return true;
        }

        return false;
    }

    /**
     * 相同
     */
    alike(idA,idB,pThis){
        let rowA=pThis.treeA.data.list[0].list[idA]
        pThis.addData(new Row(rowA.find(1),rowA.find(4)),pThis.treeA.data.list[idA])
        this.isErgodic=true;
    }

    /**
     * A 结构里面存在
     * B 结构里面没有
     */
    haveA(){

    }

    /**
     * B 结构存在
     * A 结构不存在
     */
    haveB(idA,idB,pThis,pid){
        let rowA=pThis.treeA.data.list[0].list[pid]
        let rowB=pThis.treeB.data.list[0].list[idB]
        pThis.addData(new Row(rowA.find(1),rowB.find(4)),pThis.treeB.data.list[idB])

        if(!rowB.isZero(2)){
            this. ergodicAddTreeB (rowB.find(2))
        }

    }

    /**
     *   遍历B树 添加B
     * @param w
     * @returns {Array}
     */
    ergodicAddTreeB (idB) {
        let _this=this;
        _this.copyB(idB)
        if (this.pThis.struB[idB].isZero(2)) {

            if (!this.pThis.struB[idB].isZero(3)) {
                this. ergodicAddTreeB(this.pThis.struB[idB].find(3));
            }
        } else {
            this. ergodicAddTreeB(this.pThis.struB[idB].find(2))
            if (!this.pThis.struB[idB].isZero(3)) {
                this. ergodicAddTreeB(this.pThis.struB[idB].find(3));

            }
        }
    }

    copyB(idB){
        let rowB=this.pThis.treeB.data.list[0].list[idB]
        this. pThis.addData(new Row(rowB.find(1),rowB.find(4)),this.pThis.treeB.data.list[idB])
    }
}